package com.yihuan.controller;

import com.yihuan.entity.CodeEntity;
import com.yihuan.entity.CompanyEntity;
import com.yihuan.entity.MailEntity;
import com.yihuan.entity.UserEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.service.CodeService;
import com.yihuan.service.CompanyService;
import com.yihuan.service.MailService;
import com.yihuan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Author:梦醒
 * Date: 2022/1/28 9:04
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class MailController {
    private Map<String, Object> map = new HashMap<>();
    @Autowired
    CompanyService companyService;
    @Autowired
    MailService mailService;
    @Autowired
    CodeService codeService;
    @Autowired
    UserService userService;
    
    /**
     * 发送密钥
     *
     * @param mails
     * @return
     * @throws Exception
     */
    @RequestMapping("/insertCodeByCodeData")
    public Map<String, Object> insertCodeByCodeData(@RequestBody MailEntity mails) throws Exception {
        CompanyEntity companyEntity = new CompanyEntity();
        companyEntity.setCompID(mails.getId());
        //查询编号
        List<CompanyEntity> list = companyService.findCompanyByCompID(companyEntity);
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        char[] c = s.toCharArray();
        Random random = new Random();
        String key = String.valueOf(num);
        for (int i = 0; i < 80; i++) {
            key = key + c[random.nextInt(c.length)];
        }
        //系统发送账号
        mails.setFrom("wh508377132@163.com");
        //发送邮件内容
        mails.setContent("请尽快输入商铺密钥：" + key);
        System.out.println(key);
        //发送邮件标题
        mails.setSubject("重要通知：");
        //发送邮件收件人的账号
        mails.setTo(list.get(0).getCompEmail());
        //邮件接收人的姓名
        mails.setName(list.get(0).getCompName());
        //邮件的发送的调用函数
        mailService.sendSimpleMail(mails.getFrom(), mails.getTo(), mails.getTo(), mails.getSubject(), mails.getContent(), mails.getName());
        CodeEntity codeEntity = new CodeEntity();
        codeEntity.setCodeName(key);
        codeEntity.setCodeState(1);
        codeEntity.setCompID(mails.getId());
        int insertCodeByCodeData = codeService.insertCodeByCodeData(codeEntity);
        //修改成功
        if (insertCodeByCodeData == 1) {
            map.put("code", MessageConstant.SUCCESS_CODE);
            map.put("explain", MessageConstant.COMPANY_SEND_CODE_SUCCESS);
            return map;
        }
        map.put("code", MessageConstant.FALL_CODE);
        map.put("explain", MessageConstant.COMPANY_SEND_CODE_FALL);
        return map;
    }
    
    /**
     * 发送回收员的账号和密码
     *
     * @param mails
     * @return
     * @throws Exception
     */
    @RequestMapping("/sendUserLoginNameANDUserPassword")
    public Map<String, Object> sendUserLoginNameANDUserPassword(@RequestBody MailEntity mails) throws Exception {
        UserEntity userEntity = new UserEntity();
        userEntity.setUserID(mails.getId());
        //查询编号
        List<UserEntity> list = userService.findUserByUserID(userEntity);
        //系统发送账号
        mails.setFrom("wh508377132@163.com");
        //发送邮件内容
        mails.setContent("请查收你的账号：" + list.get(0).getUserLoginName() + "\n" + "密码：" + list.get(0).getUserPassword());
        //发送邮件标题
        mails.setSubject("账号通知：");
        //发送邮件收件人的账号
        mails.setTo(list.get(0).getUserEmail());
        //邮件接收人的姓名
        mails.setName(list.get(0).getUserName());
        //邮件的发送的调用函数
        mailService.sendSimpleMail_1(mails.getFrom(), mails.getTo(), mails.getTo(), mails.getSubject(), mails.getContent(), mails.getName());
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain", MessageConstant.COMPANY_SEND_CODE_SUCCESS);
        return map;
    }
    
    
}
